home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / Portable Patmos / src / portable kernel / mac / progress.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-30  |  11.3 KB  |  558 lines  |  [TEXT/KAHL]

  1. #ifdef THINK_C
  2. #define start main
  3. #endif
  4.  
  5. #include <string.h>
  6.  
  7. #include <Files.h>
  8. #include <AppleEvents.h>
  9. #include <Quickdraw.h>
  10. #include <Menus.h>
  11. #include <Windows.h>
  12. #include <ToolUtils.h>
  13. #include <Desk.h>
  14. #include <SegLoad.h>
  15. #include <OSUtils.h>
  16. #include <Memory.h>
  17. #include <Fonts.h>
  18. #include <Events.h>
  19. #include <OSEvents.h>
  20. #include <TextEdit.h>
  21. #include <Dialogs.h>
  22. #include <Aliases.h>
  23. #include <Folders.h>
  24. #include <Errors.h>
  25. #include <GestaltEqu.h>
  26. #include <StandardFile.h>
  27.  
  28. OSErr GotRequiredParams(AppleEvent *);
  29. void scanfolder(FSSpec *);
  30. void main(void);
  31. void SetUpMenus(void);
  32. void AdjustMenus(void);
  33. void HandleMenu(long);
  34. void SetUpWindow(void);
  35. void InitMacintosh(void);
  36. void HandleMouseDown(EventRecord *);
  37. void HandleEvent(void);
  38. void    UpdateFileName(FSSpec *name);
  39. void BeginProcessing(void);
  40. void EndProcessing(void);
  41. void ProcessDoc (FSSpec *curDocFSS);
  42. void HandleMenu (long mSelect);
  43. void SetUpMenus(void);
  44. void InitMacintosh(void);
  45. void HandleMouseDown (EventRecord *theEvent);
  46. void HandleEvent(void);
  47. void SetUpWindow(void);
  48. void ChkOsErr(OSErr);
  49.  
  50. int debug = 0;
  51.  
  52. TEHandle    TEH;
  53. #ifdef PROGRESSWINDOW
  54. WindowPtr    progressWindow;
  55. Rect        dragRect;
  56. Rect        windowBounds = { 40, 40, 200, 300 };
  57. #endif
  58.  
  59. void ChkOsErr(OSErr errcode)
  60.     {
  61.     char errno[9],*p = &errno[sizeof(errno)];
  62.     if (errcode < 0)
  63.         {
  64.         errcode = -errcode;
  65.         while (errcode > 0)
  66.             {
  67.             *--p = errcode%10+'0';
  68.             errcode /= 10;
  69.             }
  70.         *--p = '-';
  71.         *--p = &errno[sizeof(errno)]-p;
  72.         ParamText((void *)p, 0, 0, 0);
  73.         Alert(129, 0);
  74.         ExitToShell();
  75.         }
  76.     }
  77.  
  78. OSErr GotRequiredParams (AppleEvent *theEvent)
  79.     {
  80.     DescType    returnedType;
  81.     long            actualSize;
  82.     OSErr        err;
  83.     
  84.     err = AEGetAttributePtr (theEvent, keyMissedKeywordAttr, typeWildCard, &returnedType, NULL, 0, &actualSize);
  85.     if (err == errAEDescNotFound)
  86.         return noErr;
  87.     else if (err == noErr)
  88.         return errAEEventNotHandled;
  89.     else
  90.         return err;
  91.     }
  92.  
  93. void HandleOapp (long refcon, AppleEvent *theReply, AppleEvent *theAppleEvent, OSErr errplace)
  94.     {
  95.     errplace = noErr;
  96. #ifdef THINK_C
  97.     asm {
  98.     UNLK      A6
  99.     MOVEA.L   (A7)+,A0
  100.     LEA       12(A7),A7
  101.     JMP       (A0)
  102.     }
  103. #else
  104.     asm ("\n"
  105.     "unlk      a6\n"
  106.     "movel   a7@+,a0\n"
  107.     "lea       a7@(12),a7\n"
  108.     "jmp       a0@\n"
  109.     );    
  110. #endif
  111.     }
  112.  
  113. void HandleOdoc (long refcon, AppleEvent *theReply, AppleEvent *theAppleEvent, OSErr err)
  114.     {
  115.     AEDescList        docList;
  116.     
  117.     err = AEGetParamDesc (theAppleEvent, keyDirectObject, typeAEList, &docList);
  118.     if (err == noErr) 
  119.         {
  120.         err = GotRequiredParams (theAppleEvent);
  121.         if (err == noErr)
  122.             {
  123.             long            numDocs, i, actualSize;
  124.             Boolean        wasAlias,isFolder;
  125.             OSErr        err;
  126.             AEKeyword    keyword;
  127.             DescType    returnedType;
  128.     
  129.             err = AECountItems (&docList, &numDocs);
  130.             if (err == noErr) 
  131.                 {
  132.                 BeginProcessing ();
  133.                 for (i = 1; i <= numDocs; i++) 
  134.                     {
  135.                     FSSpec curDocFSS;
  136.                     err = AEGetNthPtr (&docList, i, typeFSS, &keyword, &returnedType,
  137.                                 (Ptr) &curDocFSS, sizeof (FSSpec), &actualSize);
  138.                     if (err == noErr) 
  139.                         {
  140.                         err = ResolveAliasFile (&curDocFSS, true, &isFolder, &wasAlias);
  141.                         if (err == noErr) 
  142.                             {
  143.                             if (isFolder) scanfolder(&curDocFSS);
  144.                             else ProcessDoc(&curDocFSS);
  145.                             }
  146.                         }
  147.                     }
  148.                 err = noErr;
  149.                 EndProcessing ();
  150.                 }
  151.             }
  152.         (void) AEDisposeDesc (&docList);
  153.         }
  154. #ifdef THINK_C
  155.     asm {
  156.     UNLK      A6
  157.     MOVEA.L   (A7)+,A0
  158.     LEA       12(A7),A7
  159.     JMP       (A0)
  160.     }
  161. #else
  162.     asm ("\n"
  163.     "unlk      a6\n"
  164.     "movel   a7@+,a0\n"
  165.     "lea       a7@(12),a7\n"
  166.     "jmp       a0@\n"
  167.     );    
  168. #endif
  169.     }
  170.  
  171. void HandlePdoc (long refcon, AppleEvent *theReply, AppleEvent *theAppleEvent, OSErr errplace)
  172. {
  173.     errplace = errAEEventNotHandled;
  174. #ifdef THINK_C
  175.     asm {
  176.     UNLK      A6
  177.     MOVEA.L   (A7)+,A0
  178.     LEA       12(A7),A7
  179.     JMP       (A0)
  180.     }
  181. #else
  182.     asm ("\n"
  183.     "unlk      a6\n"
  184.     "movel   a7@+,a0\n"
  185.     "lea       a7@(12),a7\n"
  186.     "jmp       a0@\n"
  187.     );    
  188. #endif
  189. }
  190.  
  191. void HandleQuit (long refcon, AppleEvent *theReply, AppleEvent *theAppleEvent, OSErr errplace)
  192. {
  193.     _exit(0);
  194.     errplace = noErr;
  195. #ifdef THINK_C
  196.     asm {
  197.     UNLK      A6
  198.     MOVEA.L   (A7)+,A0
  199.     LEA       12(A7),A7
  200.     JMP       (A0)
  201.     }
  202. #else
  203.     asm ("\n"
  204.     "unlk      a6\n"
  205.     "movel   a7@+,a0\n"
  206.     "lea       a7@(12),a7\n"
  207.     "jmp       a0@\n"
  208.     );    
  209. #endif
  210. }
  211.  
  212. void ReturnEventNotHandled (long refcon, AppleEvent *theReply, AppleEvent *theAppleEvent, OSErr errplace)
  213. {
  214.     errplace = errAEEventNotHandled;
  215. #ifdef THINK_C
  216.     asm {
  217.     UNLK      A6
  218.     MOVEA.L   (A7)+,A0
  219.     LEA       12(A7),A7
  220.     JMP       (A0)
  221.     }
  222. #else
  223.     asm ("\n"
  224.     "unlk      a6\n"
  225.     "movel   a7@+,a0\n"
  226.     "lea       a7@(12),a7\n"
  227.     "jmp       a0@\n"
  228.     );    
  229. #endif
  230. }
  231.  
  232. OSErr   CurrentProcessLocation(FSSpec *applicationSpec)
  233. {
  234.     ProcessSerialNumber currentPSN;
  235.     ProcessInfoRec info;
  236.     
  237.     currentPSN.highLongOfPSN = 0;
  238.     currentPSN.lowLongOfPSN = kCurrentProcess;
  239.     info.processInfoLength = sizeof(ProcessInfoRec);
  240.     info.processName = NULL;
  241.     info.processAppSpec = applicationSpec;
  242.     return ( GetProcessInformation(¤tPSN, &info) );
  243. }
  244.  
  245. void test_standalone(void)
  246.         {
  247.         FSSpec applicationSpec;
  248.         OSErr err = CurrentProcessLocation(&applicationSpec);
  249.         CInfoPBRec  cPB;
  250.         cPB.hFileInfo.ioNamePtr = applicationSpec.name;
  251.         cPB.hFileInfo.ioVRefNum = applicationSpec.vRefNum;
  252.         cPB.hFileInfo.ioDirID = applicationSpec.parID;
  253.         cPB.hFileInfo.ioFDirIndex = 0;
  254.  
  255.         if (!PBGetCatInfoSync(&cPB))
  256.             {
  257.             long size = cPB.hFileInfo.ioFlLgLen;
  258.             if (size)
  259.                 {
  260.                   BeginProcessing();
  261.                   ProcessDoc(&applicationSpec);
  262.                   EndProcessing();
  263.                 }
  264.             }
  265.         }
  266.  
  267. extern long *system_vectors;
  268.     
  269. void start(void)
  270.     {
  271. //    test_qd();
  272.     InitMacintosh();
  273.     probe();
  274.     SetUpMenus();
  275. /* maintain compatibility with non universal headers */
  276. #ifndef NewAEEventHandlerProc
  277. #define NewAEEventHandlerProc(x) x
  278. #endif
  279.  
  280.     AEInstallEventHandler (kCoreEventClass, kAEOpenApplication, NewAEEventHandlerProc(HandleOapp), 0, false);
  281.     AEInstallEventHandler (kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerProc(HandleOdoc), 0, false);
  282.     AEInstallEventHandler (kCoreEventClass, kAEPrintDocuments, NewAEEventHandlerProc(HandlePdoc), 0, false);
  283.     AEInstallEventHandler (kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc(HandleQuit), 0, false);
  284.  
  285.     test_standalone();
  286. #ifdef PROGRESSWINDOW
  287.     SetUpWindow();
  288. #endif
  289.     system_vectors[32] = (long)trap_patch_0();
  290.     asm    { 
  291.         moveq    #0,d0
  292.         trap #0
  293.         };
  294. }
  295. /* end main */
  296.  
  297.  
  298.  
  299. MenuHandle    appleMenu, fileMenu, editMenu;
  300.  
  301. enum    {
  302.     appleID = 1,
  303.     fileID,
  304.     editID
  305.     };
  306.  
  307. enum    {
  308.     openItem = 1,
  309.     closeItem,
  310.     quitItem = 4
  311.     };
  312.  
  313. void SetUpMenus(void)
  314. {
  315.     InsertMenu(appleMenu = NewMenu(appleID, (unsigned char *)"\001\024"), 0);
  316.     InsertMenu(fileMenu = NewMenu(fileID, (unsigned char *)"\004File"), 0);
  317.     DrawMenuBar();
  318.     AppendMenu(appleMenu, (unsigned char *)"\011About ...");
  319.     AddResMenu(appleMenu, 'DRVR');
  320.     AppendMenu(fileMenu, (unsigned char *)"\pOpen/O;Flags/L;Quit/Q;Toggle debug/D");
  321. }
  322. /* end SetUpMenus */
  323.  
  324. void HandleMenu(long mSelect)
  325. {
  326.     int            menuID = HiWord(mSelect);
  327.     int            menuItem = LoWord(mSelect);
  328.     Str255        name;
  329.     GrafPtr        savePort;
  330.     WindowPeek    frontWindow;
  331.     
  332.     switch (menuID)
  333.       {
  334.       case    appleID:
  335.           switch(menuItem)
  336.               {
  337. #ifdef PROGRESSWINDOW
  338.               case 1: 
  339.                   {
  340.                   FSSpec new;
  341.                   BlockMove("\005Hello", new.name, 6);
  342.                   UpdateFileName(&new);
  343.                 }
  344.               break;
  345. #endif
  346.               default:
  347.             GetPort(&savePort);
  348.             GetItem(appleMenu, menuItem, name);
  349.             OpenDeskAcc(name);
  350.             SetPort(savePort);
  351.             }
  352.         break;
  353.     
  354.       case    fileID:
  355.           switch(menuItem)
  356.               {
  357.               case 1: 
  358.                   {
  359.                 long        aProcID;
  360.                   SFReply macSFReply;
  361.                   FSSpec new;
  362.                 Point    where;
  363.                 SFTypeList    typeList;
  364.             
  365.                 typeList[0]='TEXT';    /* file type to search for */
  366.                 typeList[1]='ŎŎ';    /* file type to search for */
  367.                 where.h=20;  where.v=90;    /* SF dialog window position */
  368.             
  369.                 SFGetFile(where,        /* put topLeft corner of dialog box here */
  370.                             (ConstStr255Param)0,            /* always 0 on SFGetFile() */
  371.                             (ProcPtr)0,            /* address of custom filter */
  372.                             2,            /* looking for two file types */
  373.                             typeList,    /* start of list of file types */
  374.                             (ProcPtr)0,     /* use standard dialog handler */
  375.                             &macSFReply);    /* put results here */
  376.  
  377.                 if ( macSFReply.good ) 
  378.                     { 
  379.                     WDPBRec    wdpb;
  380.                     wdpb.ioNamePtr = 0;
  381.                     wdpb.ioVRefNum = macSFReply.vRefNum;
  382.                     wdpb.ioWDIndex =0;
  383.                     wdpb.ioWDProcID =0;
  384.                     wdpb.ioWDVRefNum =0;
  385.                     PBGetWDInfoSync( &wdpb );
  386.                     new.parID = wdpb.ioWDDirID;
  387.                     new.vRefNum = wdpb.ioWDVRefNum;
  388.                       BlockMove(macSFReply.fName, new.name, macSFReply.fName[0]+1);
  389.                       BeginProcessing();
  390.                       ProcessDoc(&new);
  391.                       EndProcessing();
  392.                     }
  393.                   }
  394.               break;
  395.             case 2: 
  396.                 {
  397. //                crt_getargs();
  398.                 break;
  399.                 }
  400.             case 3: _exit(0);
  401.             case 4: debug = !debug; break;
  402.             }
  403.         break;
  404.                   
  405.       }
  406. }
  407. /* end HandleMenu */
  408.  
  409. #ifdef PROGRESSWINDOW
  410.  
  411. void SetUpWindow(void)
  412.  
  413. {
  414.     dragRect = qd.screenBits.bounds;
  415.     
  416.     progressWindow = NewWindow(0L, &windowBounds, (unsigned char *)"\010progress", 
  417.         true, noGrowDocProc, (WindowPtr) -1L, true, 0);
  418.     SetPort(progressWindow);
  419.     TEH = TENew(&windowBounds, &windowBounds);
  420. }
  421. /* end SetUpWindow */
  422.  
  423. #endif
  424.  
  425.  
  426. void InitMacintosh(void)
  427.  
  428. {
  429.     MaxApplZone();
  430.     
  431.     InitGraf(&qd.thePort);
  432.     InitFonts();
  433.     FlushEvents(everyEvent, 0);
  434.     InitWindows();
  435.     InitMenus();
  436.     TEInit();
  437.     InitDialogs(0L);
  438.     InitCursor();
  439.  
  440. }
  441. /* end InitMacintosh */
  442.  
  443. #if 0
  444.  
  445. void HandleMouseDown (EventRecord    *theEvent)
  446.  
  447. {
  448.     WindowPtr    theWindow;
  449.     int            windowCode = FindWindow (theEvent->where, &theWindow);
  450.     
  451.     switch (windowCode)
  452.       {
  453.       case inSysWindow: 
  454.         SystemClick (theEvent, theWindow);
  455.         break;
  456.         
  457.       case inMenuBar:
  458.         HandleMenu(MenuSelect(theEvent->where));
  459.         break;
  460.  
  461. #ifdef PROGRESSWINDOW
  462.         
  463.       case inDrag:
  464.           if (theWindow == progressWindow)
  465.             DragWindow(progressWindow, theEvent->where, &dragRect);
  466.             break;
  467.             
  468.       case inContent:
  469.           if (theWindow == progressWindow)
  470.             {
  471.             if (theWindow != FrontWindow())
  472.               SelectWindow(progressWindow);
  473.             else
  474.               InvalRect(&progressWindow->portRect);
  475.             }
  476.           break;
  477.           
  478.       case inGoAway:
  479.           if (theWindow == progressWindow && 
  480.               TrackGoAway(progressWindow, theEvent->where))
  481.           HideWindow(progressWindow);
  482.             break;
  483. #endif
  484.       }
  485. }
  486. /* end HandleMouseDown */
  487.  
  488. void HandleEvent(void)
  489. {
  490.     int            ok;
  491.     EventRecord    theEvent;
  492.  
  493.     HiliteMenu(0);
  494.     SystemTask ();        /* Handle desk accessories */
  495.     
  496.     ok = GetNextEvent (everyEvent, &theEvent);
  497.     if (ok)
  498.       switch (theEvent.what)
  499.         {
  500.         case mouseDown:
  501.             HandleMouseDown(&theEvent);
  502.             break;
  503.             
  504.         case keyDown: 
  505.         case autoKey:
  506.             if ((theEvent.modifiers & cmdKey) != 0)
  507.               {
  508.               HandleMenu(MenuKey((char) (theEvent.message & charCodeMask)));
  509.               }
  510.             break;
  511.  
  512. #ifdef PROGRESSWINDOW
  513.             
  514.         case updateEvt: if (progressWindow)
  515.             {
  516.             Rect upper = progressWindow->portRect;
  517.             Rect lower = progressWindow->portRect;
  518.             upper.bottom = upper.top + 32;
  519.             lower.top = upper.bottom;
  520.             BeginUpdate(progressWindow);
  521.             SetPort(progressWindow);
  522.             EraseRect(&lower);
  523.             upper.right = lower.right;
  524.             FillRect(&upper,(void *)&qd.white);
  525.             TEUpdate(&progressWindow->portRect, TEH);
  526.             EndUpdate(progressWindow);
  527.             break;
  528.             }
  529.         case activateEvt:
  530.             InvalRect(&progressWindow->portRect);
  531.             break;
  532. #endif
  533.  
  534.         case kHighLevelEvent:
  535.             AEProcessAppleEvent (&theEvent);
  536.             break;
  537.         }
  538. }
  539. /* end HandleEvent */
  540.  
  541. #endif
  542. #ifdef PROGRESSWINDOW
  543.  
  544. void    UpdateFileName(FSSpec *new)
  545.     {
  546.     if (progressWindow)
  547.         {
  548.         Rect upper = progressWindow->portRect;
  549.         Rect lower = progressWindow->portRect;
  550.         upper.bottom = upper.top + 32;
  551.         lower.top = upper.bottom;
  552.         TESetText(&(new->name[1]),new->name[0],TEH);
  553.         InvalRect(&lower);
  554.         HandleEvent();
  555.         }
  556.     }
  557.  
  558. #endif